function TreeNode(val) {
    this.val = val;
    this.left = null;
    this.right = null;
}

var buildTree = function(preorder, inorder) {
    // 边界条件
    if (preorder.length == 0) return null
    // 获取根节点
    let rootval = new TreeNode(preorder[0])
    let index = inorder.indexOf(preorder[0])
    rootval.left = buildTree(preorder.slice(1,index + 1), inorder.slice(0, index))
    rootval.right = buildTree(preorder.slice(index + 1), inorder.slice(index + 1))
    return rootval
}